/**
 * Created by ruiyuan on 15-7-23.
 */
var qiniu = require('qiniu');
var uuid = require('uuid');

qiniu.conf.ACCESS_KEY = qiniuAccessKey;//qiniu_config是我的配置文件
qiniu.conf.SECRET_KEY = qiniuSecretKey;


function getKey() {
    return 'images/' + uuid.v1() + '.webp';
}

function token() {
    var putPolicy = new qiniu.rs.PutPolicy(qiniuBucket);
    putPolicy.transform = 'imageView2/2/format/webp';
    putPolicy.fopTimeout = 500;

    return putPolicy.token();
}

exports.uploadFile = function (path, callback) {
    var extra = new qiniu.io.PutExtra();

    qiniu.io.putFile(token(), getKey(), path, extra, function (err, ret) {
        if (!err) {
            // 上传成功， 处理返回值

            callback(null, '/' + ret.key);
            // ret.key & ret.hash
        } else {
            // 上传失败， 处理返回代码
            logger.error('qiniu upload file error ----' + err.message);
            callback(err, null);
        }
    });
};
